<?php
include_once '../init.inc.php';
if ($log->admincheck($_SESSION['loggedin'], "logon", "password", "useremail", $_SESSION['loggedusr']) == false){
    die(include("retry.php"));
    };

include_once 'header.php';


ob_start(array('HTML_FormPersister', 'ob_formPersisterHandler'));
?>

         <form>
             <table class="main_table">
                 <thead>
                 <th>*</th>
                 <th>Вопрос</th>
                </thead>
<?php
 $sql = "SELECT p.pollID, p.pollQuestion, a.pollAnswerPoints
        FROM `polls` AS p
        LEFT JOIN `pollanswers` AS a ON p.pollID = a.pollID";

 $result = mysql_query($sql) or die('Error, insert query failed');
 
 
 
                while ($row = mysql_fetch_assoc($result)) { ?>
                                    <tr>
                                        <td>
                                            <!--<a id="<?php //echo $row['ID'];?>" href="#" class="addCart">Add</a>-->
                                       
                                                <input type="radio" name="ID" value="<?php echo $row['pollID'] ?>">
                                                 
                                           
                                            <span id="pollAnswer<?=$row['pollID']?>" class="tally-total"><?php echo "(".$row['pollAnswerPoints'].")"; ?></span>
                                        
                                        </td>
                                        
                    <?php    foreach ($row as $key=>$cellValue){ 
                                        if ($key=="pollID") continue;
                                        if ($key=="pollAnswerPoints") continue;
                                        if ($key=="pollQuestion")  {?>
                                        <td class="left-align">
                                           
                                              <?php echo excerpt($cellValue); ?>
                                          
                                        </td>
                                         <?php }else { ?>
                                             <td>
                                           
                                              <?php 
                                              
                                              echo excerpt($cellValue); ?>
                                          
                                             </td>
                                     
                                             
                                       <?php       
                                         }
                                                             
                                                                } 
                                        
                                       
                                ?></tr> <?php
                   }   
 
 
                   ?>         <tr>
                                        <td>                              
                                    </td><td class="left-align"><input type=text  name="request"><input type=submit value="Добавить" name="add"></td></tr>
             </table>
             <div style="float:right;">
                                    
             </div>
             <input type=submit value="Удалить" name="del"> 
     </form>
       
     

 <?php 
 
 if (@$_REQUEST['del']){
     // удалить записи из таблиц polls pollanswers userpoll где pollId=pollId
     $pollId = (int)$_REQUEST['ID'];
     if (!$pollId) exit;
     
     $sql = "DELETE polls FROM polls WHERE polls.pollID = $pollId";
     $result = mysql_query($sql) or die('Error, insert query failed');
     $sql = "DELETE pollanswers FROM pollanswers WHERE pollanswers.pollID = $pollId";
     $result = mysql_query($sql) or die('Error, insert query failed');
     $sql = "DELETE userpoll FROM userpoll WHERE userpoll.pollID = $pollId";
     $result = mysql_query($sql) or die('Error, insert query failed');
     
     
     
     echo "<script>location.href=\"admin_poll.php\";</script>";
     

 }
  if (@$_REQUEST['add']){
      
$tema = mysql_real_escape_string($_REQUEST['request']);
     
        $sql = "INSERT INTO `polls` SET pollQuestion='$tema', pollStatus=1";
          $result = mysql_query($sql) or die('Error, insert query failed');
     
  echo "<script>location.href=\"admin_poll.php\";</script>";

 }

       
       
       $sql =  "SELECT p.pollID, p.pollQuestion, u.usermail, u.poll_date
                FROM `polls` AS p
                LEFT JOIN `userpoll` AS u ON p.pollID = u.pollID
                ORDER BY p.pollQuestion";
       
        $result = mysql_query($sql) or die('Error, insert query failed');
        ?>

             <table class="vote_table">
                 <thead>
                 <th>Вопрос</th>
                 <th>User</th>
                 <th>Date time</th>
                </thead>

<?php
        
                    // вывод голосовавших
                        while ($row = mysql_fetch_assoc($result)) { ?>
                                    <tr>
         
                                        
                    <?php    foreach ($row as $key=>$cellValue){ 
                                        if ($key=="pollID") continue;
                                        if ($key=="pollQuestion")  {?>
                                        <td class="left-align">
                                           
                                              <?php echo excerpt($cellValue); ?>
                                          
                                        </td>
                                         <?php }else { ?>
                                             <td>
                                           
                                              <?php 
                                              
                                              echo excerpt($cellValue); ?>
                                          
                                             </td>
                                     
                                             
                                       <?php       
                                         }
                                                             
                                                                } 
                                        
                                       
                                ?></tr> 

        
                                    
                                    <?php
                   } 
       

 
 
 ?>
    </table>            

                <?php
 
        include_once 'footer.php';
      //  print_r($_REQUEST);
 ?>